package com.prolestar.publicsentiment.domain;

import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener;
import com.prolestar.Enum.PublicsentimentAttentionStatus;
import com.prolestar.Enum.PublicsentimentDeleteStatus;
import com.prolestar.common.exception.ServiceException;
import com.prolestar.common.utils.DateUtils;
import com.prolestar.common.utils.DictUtils;
import com.prolestar.common.utils.SecurityUtils;
import com.prolestar.common.utils.StringUtils;
import com.prolestar.publicsentiment.mapper.PsPublicsentimentMapper;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class PsListener extends AnalysisEventListener<PsPublicsentimentExc> {


    /**
     * 缓存大小
     */
    private static final int BATCH_SIZE = 100;

    /**
     * 缓存数据
     */
    private List<T> cacheList = new ArrayList<>(BATCH_SIZE);


    private final PsPublicsentimentMapper psPublicsentimentMapper;



    public PsListener(PsPublicsentimentMapper psPublicsentimentMapper){
        this.psPublicsentimentMapper = psPublicsentimentMapper;
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        exception.printStackTrace();
    }

    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        // TODO Auto-generated method stub
        CellExtraTypeEnum e = extra.getType();
        super.extra(extra, context);
    }

    @Override
    public void invoke(PsPublicsentimentExc data, AnalysisContext context)  {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            if(StringUtils.isNull(data)){
                throw new ServiceException("不能导入空数据！");
            }
            try {
                PsPublicsentiment publicsentiment = copy(data);
                psPublicsentimentMapper.insertPsPublicsentiment(publicsentiment);
                successNum++;
                successMsg.append("<br/>" + successNum + "、成功 ");
            } catch (ParseException e) {
                failureNum++;
                failureMsg.append("<br/>" + successNum + "、失败 ");
            }
        }




    @Override
    public boolean hasNext(AnalysisContext context) {
        return true;
    }

    /**
     * 读的时候，每读取excel一行记录就会调用监听器的invoke()方法
     * @param user
     * @param analysisContext
     */


    /**
     * 整个过程结束后执行该方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("整个过程结束后执行该方法");
    }

    private PsPublicsentiment copy(PsPublicsentimentExc psPublicsentimentExc) throws ParseException {
        PsPublicsentiment publicsentiment = new PsPublicsentiment();
        String tendentiousness = null;
        String media = null;
        String releaseArea = null;
        String sourceRange = null;
        if (StringUtils.isNotEmpty(psPublicsentimentExc.getTendentiousness())){

             tendentiousness = DictUtils.getDictValue("ps_tendentiousness", psPublicsentimentExc.getTendentiousness());
        }
        if (StringUtils.isNotEmpty(psPublicsentimentExc.getMediaType())){
            media = DictUtils.getDictValue("ps_media", psPublicsentimentExc.getMediaType());
        }
        if (StringUtils.isNotEmpty(psPublicsentimentExc.getReleaseArea())){
            releaseArea = DictUtils.getDictValue("ps_possession", psPublicsentimentExc.getReleaseArea());
        }
        if (StringUtils.isNotEmpty(psPublicsentimentExc.getSourceRange())){
            sourceRange = DictUtils.getDictValue("ps_message", psPublicsentimentExc.getSourceRange());
        }
        publicsentiment.setPstitle(psPublicsentimentExc.getPsTitle());
//        publicsentiment.setLinkUrl(psPublicsentimentExc.getLinkUrl());
        publicsentiment.setSrcWebset(psPublicsentimentExc.getSrcWebset());
        try {
            publicsentiment.setReleaseTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(psPublicsentimentExc.getReleaseTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        publicsentiment.setKeywords(psPublicsentimentExc.getKeywords());
        publicsentiment.setPscontent(psPublicsentimentExc.getPscontent());
        publicsentiment.setNewsAuthor(psPublicsentimentExc.getNewsAuthor());
//        publicsentiment.setReadingCount(psPublicsentimentExc.getReadingCount());
//        publicsentiment.setReplyCount(psPublicsentimentExc.getReplyCount());
//        publicsentiment.setSharesCount(psPublicsentimentExc.getSharesCount());
//        publicsentiment.setLikesCount(psPublicsentimentExc.getLikesCount());
//        publicsentiment.setWbFans(psPublicsentimentExc.getWbFans());
        if (StringUtils.isNotEmpty(tendentiousness)) {
            publicsentiment.setTendentiousness(Long.parseLong(tendentiousness));
        }
        publicsentiment.setMediaType(Long.parseLong(media));
        if (StringUtils.isNotEmpty(releaseArea)) {
            publicsentiment.setReleaseArea(Long.parseLong(releaseArea));
        }
        if (StringUtils.isNotEmpty(sourceRange)) {
            publicsentiment.setSourceRange(Long.parseLong(sourceRange));
        }
        publicsentiment.setIsDelete(PublicsentimentDeleteStatus.NODELETS);
        publicsentiment.setCreateBy(SecurityUtils.getUserId().toString());
        publicsentiment.setCreateTime(DateUtils.getNowDate());
        publicsentiment.setIsAttention(PublicsentimentAttentionStatus.NOSPECIALFOCUS);
        return publicsentiment;
    }


    /**
     * 读取excel表头信息
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头信息"+headMap);
    }


}

